package com.example.shushiyunsst.fragment.main.home.recycler.provider;

import android.content.Context;
import android.view.View;

import com.example.library_common.adapter.multi.AdapterViewItemViewClick;
import com.example.library_common.adapter.multi.JssMultiItemViewHolder;
import com.example.library_common.adapter.multi.JssNewMultipleAdapter;
import com.example.library_common.album.view.grid_view.XGridLayout;
import com.example.library_common.album.view.grid_view.adapter.CommAdapter;
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder;
import com.example.shushiyunsst.R;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.FunEntity;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.FunEntityWrapper;

public class FunProvider extends JssMultiItemViewHolder<FunEntityWrapper> {

    private CommAdapter<FunEntity>mAdapter;

    public FunProvider(View view) {
        super(view);
    }

    @Override
    public int getItemLayout() {
        return R.layout.fun_provider_layout;
    }

    @Override
    public void convert(Context context, FunEntityWrapper funEntityWrapper) {
        super.convert(context, funEntityWrapper);
        XGridLayout mXGridLayout = findView(R.id.mXGridLayout);
        if (mAdapter==null){
            mAdapter= new CommAdapter<FunEntity>(context, R.layout.fun_item_provider_layout) {
                @Override
                public void convert(XQuickViewHolder holder, FunEntity item, int position) {
                    holder.setImageResource(R.id.icon,item.getIcon())
                            .setText(R.id.title_template,item.getTitle())
                    .setOnClickListener(R.id.funView, view -> {
                        if (adapter instanceof JssNewMultipleAdapter) {
                            AdapterViewItemViewClick itemViewClick =
                                    ((JssNewMultipleAdapter) adapter).getAdapterViewItemViewClick();
                            if (itemViewClick != null) {
                                itemViewClick.onAdapterViewListItemViewClick(item,item.getItemType());
                            }
                        }
                    })
                    ;
                }
            };
            if (adapter instanceof JssNewMultipleAdapter) {
                if (mXGridLayout != null) {
                    mXGridLayout.setMaxItem(funEntityWrapper.getList().size());
                    mXGridLayout.setAdapter(mAdapter,((JssNewMultipleAdapter) adapter).getLifecycle());
                }
            }
        }

        mAdapter.addAll(funEntityWrapper.getList());
    }
}
